package com.fanqj.resilience4j.service.impl;

import com.fanqj.resilience4j.service.ITestService;
import org.springframework.stereotype.Service;


/**
 * 测试接口实现类
 * @author <a href="mailto:fanqijin@wxchina.com">fanqj</a>
 * @Date 2021年01月26日 14:07
 * @Version 6.4.1
 */
@Service("iTestService")
public class TestServiceImpl implements ITestService {

    private volatile  int i = 0;

    @Override
    public String test(String name) {
        i++;
        if (i % 2 == 0) {
            throw new RuntimeException("test method having an error");
        }
        return "Hello " + name;
    }

    @Override
    public String testBulkhead(String name) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Hello " + name;
    }

    @Override
    public String testRateLimit(String name) {
        return "Hello " + name;
    }

    @Override
    public String testTimeLimit(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "success";
    }
}
